package tutorial.model.access;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.mindrot.jbcrypt.BCrypt;

@Entity
@NamedQueries({
	@NamedQuery(name = Person.GETUSERBYEMAIL, query = "SELECT p FROM Person p WHERE p.email = :email"),
	@NamedQuery(name = Person.GETUSERBYUSERNAME, query = "SELECT p FROM Person p WHERE p.username = :username"),
})
public class Person {
	
	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
    private int id;
	public static final String GETUSERBYEMAIL = "GETUSERBYEMAIL";
	public static final String GETUSERBYUSERNAME = "GETUSERBYUSERNAME";
	private String username;
	private String email;
	private String password;
	@Temporal(TemporalType.TIMESTAMP)
    private Date creationDate;
	@Lob
	private String description;
	
	public Person() {
		this.creationDate = new Date();
	}
	
	public Person(String email, String username, String password) {
		this.email = email;
        this.username = username;
		this.setPassword(password);
        this.creationDate = new Date();
    }
    
    public boolean checkPassword(String password) {
        return BCrypt.checkpw(password, this.password);
    }
    
	public void setPassword(String password) {
		this.password = BCrypt.hashpw(password, BCrypt.gensalt(10));
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public String getPassword() {
		return password;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
}
